package com.haixiaoke.saas.contract.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.PlaceholderUtil;
import com.haixiaoke.saas.contract.domain.ContractTemplate;
import com.haixiaoke.saas.contract.domain.ContractTemplateField;
import com.haixiaoke.saas.contract.mapper.ContractTemplateMapper;
import com.haixiaoke.saas.contract.service.IContractTemplateFieldService;
import com.haixiaoke.saas.contract.service.IContractTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 合同模板Service业务层处理
 *
 * @author panda
 * @date 2024-05-30
 */
@Service
public class ContractTemplateServiceImpl implements IContractTemplateService {
    @Autowired
    private ContractTemplateMapper contractTemplateMapper;
    @Autowired
    private IContractTemplateFieldService contractTemplateFieldService;

    /**
     * 查询合同模板
     *
     * @param contractTemplateId 合同模板主键
     * @return 合同模板
     */
    @Override
    public ContractTemplate selectContractTemplateByContractTemplateId(String contractTemplateId) {
        return contractTemplateMapper.selectContractTemplateByContractTemplateId(contractTemplateId);
    }

    /**
     * 查询合同模板列表
     *
     * @param contractTemplate 合同模板
     * @return 合同模板
     */
    @Override
    public List<ContractTemplate> selectContractTemplateList(ContractTemplate contractTemplate) {
        return contractTemplateMapper.selectContractTemplateList(contractTemplate);
    }

    /**
     * 新增合同模板
     *
     * @param contractTemplate 合同模板
     * @return 结果
     */
    @Override
    public int insertContractTemplate(ContractTemplate contractTemplate) {
        if (ObjectUtil.isNotEmpty(contractTemplate.getCopyContractTemplateId())) {
            ContractTemplate copyContractTemplate = contractTemplateMapper.selectContractTemplateByContractTemplateId(contractTemplate.getCopyContractTemplateId());
            contractTemplate.setTemplateContent(copyContractTemplate.getTemplateContent());
        }
        contractTemplate.setCreateTime(DateUtils.getNowDate());
        return contractTemplateMapper.insertContractTemplate(contractTemplate);
    }

    /**
     * 修改合同模板
     *
     * @param contractTemplate 合同模板
     * @return 结果
     */
    @Override
    public int updateContractTemplate(ContractTemplate contractTemplate) {
        contractTemplate.setUpdateTime(DateUtils.getNowDate());
        return contractTemplateMapper.updateContractTemplate(contractTemplate);
    }

    /**
     * 批量删除合同模板
     *
     * @param contractTemplateIds 需要删除的合同模板主键
     * @return 结果
     */
    @Override
    public int deleteContractTemplateByContractTemplateIds(String[] contractTemplateIds) {
        return contractTemplateMapper.deleteContractTemplateByContractTemplateIds(contractTemplateIds);
    }

    @Override
    public int softDeleteContractTemplateByContractTemplateIds(String[] contractTemplateIds) {
        return contractTemplateMapper.softDeleteContractTemplateByContractTemplateIds(contractTemplateIds);
    }

    /**
     * 删除合同模板信息
     *
     * @param contractTemplateId 合同模板主键
     * @return 结果
     */
    @Override
    public int deleteContractTemplateByContractTemplateId(String contractTemplateId) {
        return contractTemplateMapper.deleteContractTemplateByContractTemplateId(contractTemplateId);
    }

    @Override
    public List<ContractTemplate> selectTemplateList(ContractTemplate contractTemplate) {
        return contractTemplateMapper.selectTemplateList(contractTemplate);
    }

    @Override
    public ContractTemplate previewContractTemplateByContractTemplateId(String contractTemplateId) {
        ContractTemplate contractTemplate = contractTemplateMapper.selectContractTemplateByContractTemplateId(contractTemplateId);
        ContractTemplateField contractTemplateField = new ContractTemplateField();
        contractTemplateField.setFieldType(contractTemplate.getTemplateType());
        Map<String, Object> map = contractTemplateFieldService.selectContractTemplateFieldMap(contractTemplateField);
        String content = PlaceholderUtil.format(contractTemplate.getTemplateContent(), map, "");
        contractTemplate.setTemplateContent(content);
        return contractTemplate;
    }

    @Override
    public int updateContractTemplateContent(ContractTemplate contractTemplate) {
        ContractTemplate template = contractTemplateMapper.selectContractTemplateByContractTemplateId(contractTemplate.getContractTemplateId());
        ContractTemplateField contractTemplateField = new ContractTemplateField();
        contractTemplateField.setFieldType(template.getTemplateType());
        Map<String, Object> map = contractTemplateFieldService.selectContractTemplateFieldByNameMap(contractTemplateField);
        String content = PlaceholderUtil.format(contractTemplate.getTemplateContent(), map, PlaceholderUtil.PLACEHOLDER_FORMAT_SUPPORT);
        contractTemplate.setTemplateContent(content);
        contractTemplate.setUpdateTime(DateUtils.getNowDate());
        return contractTemplateMapper.updateContractTemplate(contractTemplate);
    }

    @Override
    public ContractTemplate selectContractByContractTemplateId(String contractTemplateId) {
        ContractTemplate contractTemplate = this.selectContractTemplateByContractTemplateId(contractTemplateId);
        ContractTemplateField contractTemplateField = new ContractTemplateField();
        contractTemplateField.setFieldType(contractTemplate.getTemplateType());
        Map<String, Object> map = contractTemplateFieldService.selectContractTemplateFieldByFieldNameMap(contractTemplateField);
        String content = PlaceholderUtil.format(contractTemplate.getTemplateContent(), map, "");
        contractTemplate.setTemplateContent(content);
        return contractTemplate;
    }
}
